package com.lsa.skienna.challenge.chap11.the111105_CuttingSticks;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;

public class TestCuttingSticks extends TestCase {
    @Override
    protected void setUp() throws Exception {
        Main.lineSep = IOUtils.LINE_SEPARATOR_UNIX;
    }
    
    public void testBounds() {
        // len 8
        List<Integer> cuts = Arrays.asList(1,3,5);
        assertEquals(new Main.Bounds(0,0), Main.findBounds(0, 0, cuts));
        assertEquals(new Main.Bounds(1,1), Main.findBounds(1, 1, cuts));
        assertEquals(new Main.Bounds(0,1), Main.findBounds(0, 1, cuts));
        assertEquals(new Main.Bounds(0,1), Main.findBounds(0, 2, cuts));
        assertEquals(new Main.Bounds(0,2), Main.findBounds(0, 3, cuts));
        assertEquals(new Main.Bounds(0,2), Main.findBounds(0, 4, cuts));
        assertEquals(new Main.Bounds(0,3), Main.findBounds(0, 5, cuts));
        assertEquals(new Main.Bounds(0,3), Main.findBounds(0, 6, cuts));
        assertEquals(new Main.Bounds(0,3), Main.findBounds(0, 7, cuts));
        
        assertEquals(new Main.Bounds(1,2), Main.findBounds(2, 3, cuts));
        assertEquals(new Main.Bounds(1,2), Main.findBounds(2, 4, cuts));
        assertEquals(new Main.Bounds(1,3), Main.findBounds(2, 5, cuts));
        assertEquals(new Main.Bounds(3,3), Main.findBounds(6, 7, cuts));
    }
    
    public void testCuttingSticks() throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Main.process(new FileInputStream("data/pc11/the111105_CuttingSticks/111105.inp"), new PrintStream(out));
        String expected = FileUtils.readFileToString(new File("data/pc11/the111105_CuttingSticks/111105.oup"));
        assertEquals(expected, out.toString());
    }
    
    public void testCuttingSticks2() throws IOException {
        String str = "1000\n15\n10 21 32 113 124 255 306 447 458 489 500 511 612 713 814\n0\n";
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes()); 
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Main.process(in, new PrintStream(out));
        assertEquals("The minimum cutting is 3558."+Main.lineSep, out.toString());
    }
    
    public void testCuttingSticks3() {
        for (int i = 0; i < 1000; i++) {
            String str = "999\r\n" + 
                    "49\r\n" + 
                    "0 20 48 68 87 105 125 146 166 185 205 229 246 261 288 309 329 345 368 383 407 422 440 461 484 500 528 549 567 588 605 621 645 666 685 709 723 745 764 783 805 825 845 862 886 900 925 940 963" + 
                    "\n0\n";
            ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes()); 
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Main.process(in, new PrintStream(out));
            assertEquals("The minimum cutting is 5649."+Main.lineSep, out.toString());
        }
    }
}
